package com.audaque.springboot.foshanupload.web.kafkademo.schedule;

/**
 * @author zgb
 * @desc ...
 * @date 2023-04-17 15:48:03
 */

import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.kafka.config.ConcurrentKafkaListenerContainerFactory;
import org.springframework.kafka.config.KafkaListenerEndpointRegistry;
import org.springframework.kafka.core.ConsumerFactory;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @description
 * @auth yh
 * @date 2022/5/11
 */
@EnableScheduling
@Component
public class CronTimer {
    /**
     * @KafkaListener注解所标注的方法并不会在IOC容器中被注册为Bean，
     * 而是会被注册在KafkaListenerEndpointRegistry中，
     * 而KafkaListenerEndpointRegistry在SpringIOC中已经被注册为Bean
     **/
    @Autowired
    private KafkaListenerEndpointRegistry registry;

    @Autowired
    private ConsumerFactory consumerFactory;


    /**
     * 定时启动监听器
     * @param
     * @author yh
     * @date 2022/5/11
     * @return
     */
    @Scheduled(cron = "*/10 * * * * ?")
    public void startListener() {
        System.out.println("启动监听器..." + DateUtil.date());
        // "scheduledConsumer"是@KafkaListener注解后面设置的监听器ID,标识这个监听器
        if (!registry.getListenerContainer("scheduledConsumer").isRunning()) {
            registry.getListenerContainer("scheduledConsumer").start();
        }
        //registry.getListenerContainer("scheduledConsumer").resume();
    }

    /**
     * 定时停止监听器
     * @param
     * @author yh
     * @date 2022/5/11
     * @return
     */
    @Scheduled(cron = "* 1 * * * ?")
    public void shutDownListener() {
        System.out.println("关闭监听器..." + DateUtil.date());
        registry.getListenerContainer("scheduledConsumer").pause();
    }

}
